$(function () {
    $.ajax("http://127.0.0.1:8080/classify/query",{
        type:"get",
        data:{
            page:1,
            size:10
        },
        success:function (result) {
            $("#tbody").html("");
            //总页数
            $("#countPage").text(result.data.pages);
            let status = result.status;
            if (status==400) {
                $("#tbody").append($("<tr>").append($("<td>").text("系统繁忙")))
            }else {
            for (let i = 0; i < result.data.data.length; i++) {
                let obj = result.data.data[i];
                $("#tbody").append(
                    $("<tr>").attr("class", "success")
                        .append($("<td>").text(obj.id)).addClass("text-center")
                        .append($("<td>").text(obj.station)).addClass("text-center")
                        .append($("<td>").text(obj.count)).addClass("text-center")
                        .append($("<td>").css("width", "100px").append($("<button class='btn btn-info'>查看</button>")).click(function () {
                            window.location.href = "information.html?jurisdiction=" + obj.station;
                        })
                        )
                        .append($("<td>").css("width", "100px")
                            .append($("<button class='btn btn-danger'>删除</button>")).click(function () {
                                // 发送post请求 删除数据
                                let data = {
                                    id: obj.id
                                };
                                $.post("http://127.0.0.1:8080/classify/delete", data, function (result) {
                                    if (result.status==400){
                                        alert("还有数据，无法删除");
                                    }else {
                                        $(this).parent("tr").remove();
                                    }
                                })

                            })
                        )
                )

            }
        }
        }
    })

    //添加
    $("#adds").click(function () {
        window.location.href = "addition.html";
    })
    $("#submit").click(function () {
        let admins = {}
        admins.name = $("#name").val();
        admins.account=$("#account").val();
        admins.password=$("#password").val();
        admins.phoneNumber=$("#phoneNumber").val();
        admins.jurisdiction=$("input:radio:checked").val();
        admins.remark=$("#remark").html();
        let data={
            data:JSON.stringify(admins)
        }
        $.post("http://127.0.0.1:8080/information/insert",data,function (result) {
            if (result==400){
                alert("账号已存在");
            }else {
            $(".box").css("display","none");
            window.location.href = "classify.html";
            }
        })
    });
    //取消添加
    $("#cancel").click(function () {
        window.location.href="classify.html";
    })

    //查看总数据
    $("#data").click(function () {
        window.location.href = "information.html";
    })

})
